﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.WmsService;
using WmsApp.StaticData;

namespace WmsApp.UI
{
    public partial class BorrowRepairEditor : DevExpress.XtraEditors.XtraUserControl
    {
        private List<RelativeCaptionConrolWrap> relativeCaptionConrolWraps = new List<RelativeCaptionConrolWrap>();
        public BorrowRepairEditor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 此方法提供：将空间加入到本对象中，可以将Control的caption的内容关联本对象的可见状态。
        /// </summary>
        /// <param name="c">需要关联的对象</param>
        /// <param name="relativeVisibleText">需要关联的对象可见时的caption</param>
        /// <param name="relativeInvisibleText">需要关联的对象不可见时的caption</param>
        public void AddRelativeCaptionConrol(Control c,string relativeVisibleText ,string relativeInvisibleText )
        {
            var wrap = new RelativeCaptionConrolWrap()
                           {
                               PControl = c,
                               RelativeInvisibleText = relativeInvisibleText,
                               RelativeVisibleText = relativeVisibleText
                           };
            relativeCaptionConrolWraps.Add(wrap);
        }

        private class RelativeCaptionConrolWrap
        {
            internal Control PControl { get; set; }
            internal string RelativeVisibleText { get; set; }
            internal string RelativeInvisibleText { get; set; }
        }

        private OutStoreBill outStoreBill = null;
        public void DataBinding(OutStoreBill outStoreBillBinding)
        {
            this.outStoreBill = outStoreBillBinding;
            this.dateEditReturnDate.DataBindings.Clear();
            this.dateEditReturnDate.DataBindings.Add("EditValue", this.outStoreBill, "ExtVarChar2", true, DataSourceUpdateMode.OnPropertyChanged);

            this.textEditTroubleReasion.DataBindings.Clear();
            this.textEditTroubleReasion.DataBindings.Add("EditValue", this.outStoreBill, "ExtVarChar3");

            this.comboBoxEditRepairTrade.DataBindings.Clear();
            this.comboBoxEditRepairTrade.DataBindings.Add("EditValue", this.outStoreBill, "RepairTrade");

            DistinctEntityBinder.BindingComboBoxEdit(comboBoxEditRepairTrade, "OutStoreBill", "RepairTrade");

            outStoreBillBinding.PropertyChanged += OutStoreBillPropertyChanged;

            OutStoreBillPropertyChanged(outStoreBillBinding, new PropertyChangedEventArgs("ExtVarChar1"));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="b"></param>
        public void SetControlEnable(bool b)
        {
            //this.labelControl2.Enabled = b;
            //this.labelControl10.Enabled = b;
            this.dateEditReturnDate.Enabled = b;
            this.textEditTroubleReasion.Enabled = b;
        }

        private void OutStoreBillPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //出库类型
            if (e.PropertyName == "ExtVarChar1")
            {
                var client = Factory.WmsServiceFactory.GetServiceClient();
                var list = client.GetOutInStoreBillTypes();
                var q = from oi in list
                        where oi.OutStoreCode == this.outStoreBill.ExtVarChar1
                        select oi;

                if(q.Count()  == 0) //如果不是特殊的业务模式，比如借用，维修
                {
                    this.labelControl2.Visible = false;
                    this.labelControl10.Visible = false;
                    this.dateEditReturnDate.Visible = false;
                    this.textEditTroubleReasion.Visible = false;
                    this.labelControl1.Visible = false;
                    this.comboBoxEditRepairTrade.Visible = false;
                    foreach (var wrap in relativeCaptionConrolWraps)
                    {
                        wrap.PControl.Text = wrap.RelativeInvisibleText;
                    }
                }
                else
                {
                    this.labelControl2.Visible = true;
                    this.labelControl10.Visible = true;
                    this.dateEditReturnDate.Visible = true;
                    this.textEditTroubleReasion.Visible = true;
                    this.labelControl1.Visible = true;
                    this.comboBoxEditRepairTrade.Visible = true;
                    foreach (var wrap in relativeCaptionConrolWraps)
                    {
                        wrap.PControl.Text = wrap.RelativeVisibleText;
                    }
                }
            
            }
        }
    }
}
